package com.tutu.controller.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.tutu.R;
import com.tutu.controller.fragment.ChatFragment;
import com.tutu.controller.fragment.ContactFragment;
import com.tutu.controller.fragment.SettingFragment;

import static com.superrtc.mediamanager.EMediaManager.initData;

/**
 * @author 图图 2020/10/11
 *
 * 主页面
 */
public class MainActivity extends FragmentActivity {

    private RadioGroup rg_main;

    private ChatFragment chatFragment;
    private ContactFragment contactFragment;
    private SettingFragment settingFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化
        initView();
        // 初始化数据
        initData();
        // 初始化监听
        initListener();
    }

    /**
     * 初始化监听
     */
    private void initListener() {
        // 默认选择会话页面
        rg_main.check(R.id.rg_main_chat);

        rg_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Fragment fragment = null;
                switch (checkedId){
                    // 聊天页面
                    case R.id.rg_main_chat:
                        fragment = chatFragment;
                        break;

                     // 联系人页面
                    case R.id.rg_main_contact:
                        fragment = contactFragment;
                        break;

                     // 设置页面
                    case R.id.rg_main_setting:
                        fragment = settingFragment;
                        break;

                    default:
                        break;
                }
                if(null != fragment) switchFragment(fragment);
            }
        });


    }

    /**
     * 切换到对应的fragment
     * @param fragment
     */
    private void switchFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 把主页面的fragment替换为选择的fragment
        fragmentManager.beginTransaction().replace(R.id.fl_main,fragment).commit();
    }

    /**
     * 创建fragment对象
     */
    private void initData() {
        chatFragment = new ChatFragment();
        contactFragment = new ContactFragment();
        settingFragment = new SettingFragment();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        rg_main = (RadioGroup)findViewById(R.id.rg_main);
    }


}
